﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.ProductionWorking;
using MangoMes.Commont.Domain.ProductionWorkOrders;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Write.API.Applications.SellStockSearchWrite;
using MediatR;
using MySql.Data.MySqlClient;

namespace MangoMes.Write.API.Applications.ProductionWrite
{
    public class ProductionCreateHandler : IRequestHandler<ProductionCreateCommand, ResultsDTO>
    {
        private readonly IBateRepository<ProductionWork> _productionRepository;
        private readonly IBateRepository<Inspected> inspected;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionRepository"></param>
        public ProductionCreateHandler(IBateRepository<ProductionWork> productionRepository, IBateRepository<Inspected> inspected)
        {
            _productionRepository = productionRepository;
            this.inspected = inspected;
        }


        /// <summary>
        /// 生产报工新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(ProductionCreateCommand request, CancellationToken cancellationToken)
        {
            DapperHelper dapperHelper = new DapperHelper();
            ResultsDTO res = new ResultsDTO { Code = 0 };
            ProductionWork per = new ProductionWork();
            per.ProductionWorkId = request.ProductionWorkId;
            per.ProductionWorkNo = request.ProductionWorkNo;
            per.ProductionTypeId = request.ProductionTypeId;
            per.ProductionWorkOrdersID = request.ProductionWorkOrdersID;
            per.PickingNode = request.PickingNode;
            per.ProductionWorkNum = request.ProductionWorkNum;
            per.departmentId = request.departmentId;
            per.ProductionWorkTime = request.ProductionWorkTime;
            per.ProductionStart = request.ProductionStart;
            per.ProductionWorkRemark = request.ProductionWorkRemark;
            per.ProductionWorkState = 0;
            string dapper = dapperHelper.MangoMesWrite;

            using (MySqlConnection conn = new MySqlConnection(dapperHelper.MangoMesWrite))
            {
                conn.Open();
                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        string str = "insert into productionwork values(@ProductionWorkId,@ProductionWorkNo,@ProductionTypeId,@ProductionWorkOrdersID,@PickingNode," +
                                   "@ProductionWorkNum,@departmentId,@ProductionWorkTime,@ProductionStart,@ProductionWorkRemark,@ProductionWorkState)";
                        res.Code = await _productionRepository.Add(str, per, dapper);
                        Inspected info= new Inspected()
                        {
                            ToBeInspectedId=0,
                            ToBeInspectedNo="TO"+DateTime.Now.ToString("yyyyMMddHHmmss"),
                            SigningNo=per.ProductionWorkNo,
                            ToBeInspectedNumber=per.ProductionWorkNum,
                            ToBeInspectedType="过程检测",
                            Address=""
                        };
                        string sql = "insert into  tobeinspected values(@ToBeInspectedId,@ToBeInspectedNo,@SigningNo,@ToBeInspectedNumber,@ToBeInspectedType,@Address)";
                        res.Code += await inspected.Add(sql, info, dapper);
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        throw;
                    }
                }
                if (res.Code > 0)
                {
                    res.Message="添加成功";
                }
                return res;
            }
        }
    }
}
